/*
 * FormUtama.java
 *
 * Created on 28 Mei 2008, 8:04
 */
package minimum.spanning.tree;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author  usu
 */
public class FormUtama extends javax.swing.JFrame {

    /*
     * Serial version UID
     */
    private static final long serialVersionUID = 1L;
    private DialogAbout dialogAbout;
    private DialogInfo dialogInfo;

    /** Creates new form FormUtama */
    public FormUtama() {
        dialogAbout = new DialogAbout(this, true);
        dialogInfo = new DialogInfo(this, true);
        setContentPane(new PanelEfect());
        initComponents();
        setLocationRelativeTo(null);

        getRootPane().getActionMap().put("UNDO", new AbstractAction() {

            /*
             * Serial version UID
             */
            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent e) {
                if (panelTree.canUndo()) {
                    panelTree.undo();
                }
            }
        });
        getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.CTRL_MASK, KeyEvent.VK_Z), "UNDO");

        getRootPane().getActionMap().put("REDO", new AbstractAction() {

            /*
             * Serial version UID
             */
            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent e) {
                if (panelTree.canRedo()) {
                    panelTree.redo();
                }
            }
        });
        getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.CTRL_MASK, KeyEvent.VK_Y), "REDO");

    }

    private void closeWindow() {
        if (JOptionPane.showConfirmDialog(this, "Anda Yakin Akan Keluar Program ?", "Keluar Program", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) {
            setVisible(false);
            JOptionPane.showMessageDialog(this, "Sampai Jumpa !!!", "Bye...", JOptionPane.INFORMATION_MESSAGE);
            System.exit(0);
        } else {
            return;
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        groupTampilkan = new javax.swing.ButtonGroup();
        panelTree = new minimum.spanning.tree.PanelTree();
        labelJudul = new javax.swing.JLabel();
        menubar = new javax.swing.JMenuBar();
        menuFile = new javax.swing.JMenu();
        itemExit = new javax.swing.JMenuItem();
        menuUbah = new javax.swing.JMenu();
        itemUndo = new javax.swing.JMenuItem();
        itemRedo = new javax.swing.JMenuItem();
        menuPohon = new javax.swing.JMenu();
        menuTampilkan = new javax.swing.JMenu();
        itemNone = new javax.swing.JRadioButtonMenuItem();
        itemAllLine = new javax.swing.JRadioButtonMenuItem();
        itemPrim = new javax.swing.JRadioButtonMenuItem();
        itemKruskal = new javax.swing.JRadioButtonMenuItem();
        itemClear = new javax.swing.JMenuItem();
        itemData = new javax.swing.JMenuItem();
        menuPertolongan = new javax.swing.JMenu();
        itemTentangkami = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Minimum Spanning Tree");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        panelTree.setBackground(new java.awt.Color(255, 255, 255));
        panelTree.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 3));
        panelTree.setOpaque(false);

        javax.swing.GroupLayout panelTreeLayout = new javax.swing.GroupLayout(panelTree);
        panelTree.setLayout(panelTreeLayout);
        panelTreeLayout.setHorizontalGroup(
            panelTreeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 758, Short.MAX_VALUE)
        );
        panelTreeLayout.setVerticalGroup(
            panelTreeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 480, Short.MAX_VALUE)
        );

        labelJudul.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        labelJudul.setForeground(new java.awt.Color(255, 255, 255));
        labelJudul.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelJudul.setText("POHON MERENTANG MINIMUM");

        menuFile.setMnemonic('F');
        menuFile.setText("File");

        itemExit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
        itemExit.setMnemonic('K');
        itemExit.setText("Keluar");
        itemExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemExitActionPerformed(evt);
            }
        });
        menuFile.add(itemExit);

        menubar.add(menuFile);

        menuUbah.setText("Ubah");
        menuUbah.addMenuListener(new javax.swing.event.MenuListener() {
            public void menuCanceled(javax.swing.event.MenuEvent evt) {
            }
            public void menuDeselected(javax.swing.event.MenuEvent evt) {
            }
            public void menuSelected(javax.swing.event.MenuEvent evt) {
                menuUbahMenuSelected(evt);
            }
        });

        itemUndo.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Z, java.awt.event.InputEvent.CTRL_MASK));
        itemUndo.setText("Undo");
        itemUndo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemUndoActionPerformed(evt);
            }
        });
        menuUbah.add(itemUndo);

        itemRedo.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Y, java.awt.event.InputEvent.CTRL_MASK));
        itemRedo.setText("Redo");
        itemRedo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemRedoActionPerformed(evt);
            }
        });
        menuUbah.add(itemRedo);

        menubar.add(menuUbah);

        menuPohon.setMnemonic('P');
        menuPohon.setText("Pohon");

        menuTampilkan.setMnemonic('T');
        menuTampilkan.setText("Tampilkan");

        itemNone.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.SHIFT_MASK));
        groupTampilkan.add(itemNone);
        itemNone.setMnemonic('T');
        itemNone.setSelected(true);
        itemNone.setText("Tak Ada");
        itemNone.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemNoneActionPerformed(evt);
            }
        });
        menuTampilkan.add(itemNone);

        itemAllLine.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.SHIFT_MASK));
        groupTampilkan.add(itemAllLine);
        itemAllLine.setMnemonic('S');
        itemAllLine.setText("Seluruh Kemungkinan");
        itemAllLine.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemAllLineActionPerformed(evt);
            }
        });
        menuTampilkan.add(itemAllLine);

        itemPrim.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, java.awt.event.InputEvent.SHIFT_MASK));
        groupTampilkan.add(itemPrim);
        itemPrim.setMnemonic('P');
        itemPrim.setText("Algoritma Prim");
        itemPrim.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemPrimActionPerformed(evt);
            }
        });
        menuTampilkan.add(itemPrim);

        itemKruskal.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_K, java.awt.event.InputEvent.SHIFT_MASK));
        groupTampilkan.add(itemKruskal);
        itemKruskal.setMnemonic('K');
        itemKruskal.setText("Algoritma Kruskal");
        itemKruskal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemKruskalActionPerformed(evt);
            }
        });
        menuTampilkan.add(itemKruskal);

        menuPohon.add(menuTampilkan);

        itemClear.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        itemClear.setMnemonic('H');
        itemClear.setText("Hapus");
        itemClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemClearActionPerformed(evt);
            }
        });
        menuPohon.add(itemClear);

        itemData.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_D, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        itemData.setMnemonic('D');
        itemData.setText("Data");
        itemData.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemDataActionPerformed(evt);
            }
        });
        menuPohon.add(itemData);

        menubar.add(menuPohon);

        menuPertolongan.setMnemonic('T');
        menuPertolongan.setText("Pertolongan");

        itemTentangkami.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        itemTentangkami.setMnemonic('T');
        itemTentangkami.setText("Tentang Kami");
        itemTentangkami.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemTentangkamiActionPerformed(evt);
            }
        });
        menuPertolongan.add(itemTentangkami);

        menubar.add(menuPertolongan);

        setJMenuBar(menubar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(labelJudul, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 764, Short.MAX_VALUE)
                    .addComponent(panelTree, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelJudul)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelTree, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-800)/2, (screenSize.height-600)/2, 800, 600);
    }// </editor-fold>//GEN-END:initComponents

private void itemNoneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemNoneActionPerformed
    panelTree.showTree(PanelTree.TREE_NONE);
}//GEN-LAST:event_itemNoneActionPerformed

private void itemAllLineActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemAllLineActionPerformed
    panelTree.showTree(PanelTree.TREE_KEMUNGKINAN);
}//GEN-LAST:event_itemAllLineActionPerformed

private void itemPrimActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemPrimActionPerformed
    panelTree.showTree(PanelTree.TREE_PRIM);
}//GEN-LAST:event_itemPrimActionPerformed

private void itemKruskalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemKruskalActionPerformed
    panelTree.showTree(PanelTree.TREE_KRUSKAL);
}//GEN-LAST:event_itemKruskalActionPerformed

private void itemClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemClearActionPerformed
    if (panelTree.getMinimumSpanningTree().size() < 1) {
        return;
    }
    if (JOptionPane.showConfirmDialog(this, "Anda Yakin Akan Menghapus Seluruh Simpul ?", "Hapus Pohon", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) {
        itemNone.setSelected(true);
        panelTree.clearTree();
    }
}//GEN-LAST:event_itemClearActionPerformed

private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
    closeWindow();
}//GEN-LAST:event_formWindowClosing

private void itemExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemExitActionPerformed
    closeWindow();
}//GEN-LAST:event_itemExitActionPerformed

private void itemTentangkamiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemTentangkamiActionPerformed
    dialogAbout.setLocationRelativeTo(this);
    dialogAbout.setVisible(true);
}//GEN-LAST:event_itemTentangkamiActionPerformed

private void itemDataActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemDataActionPerformed
    dialogInfo.setLocationRelativeTo(this);
    dialogInfo.setTreeSImpul(panelTree.getMinimumSpanningTree(), panelTree.getMinimumSpanningTreeColor());
    dialogInfo.setVisible(true);
}//GEN-LAST:event_itemDataActionPerformed

private void itemUndoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemUndoActionPerformed
    if (panelTree.canUndo()) {
        panelTree.undo();
    }
}//GEN-LAST:event_itemUndoActionPerformed

private void itemRedoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemRedoActionPerformed
    if (panelTree.canRedo()) {
        panelTree.redo();
    }
}//GEN-LAST:event_itemRedoActionPerformed

private void menuUbahMenuSelected(javax.swing.event.MenuEvent evt) {//GEN-FIRST:event_menuUbahMenuSelected
    itemRedo.setEnabled(panelTree.canRedo());
    itemUndo.setEnabled(panelTree.canUndo());
}//GEN-LAST:event_menuUbahMenuSelected

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(FormUtama.class.getName()).log(Level.SEVERE, null, ex);
                } catch (InstantiationException ex) {
                    Logger.getLogger(FormUtama.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(FormUtama.class.getName()).log(Level.SEVERE, null, ex);
                } catch (UnsupportedLookAndFeelException ex) {
                    Logger.getLogger(FormUtama.class.getName()).log(Level.SEVERE, null, ex);
                }finally{
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(FormUtama.class.getName()).log(Level.SEVERE, null, ex);
                    }finally{
                        new FormUtama().setVisible(true);
                    }
                }
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup groupTampilkan;
    private javax.swing.JRadioButtonMenuItem itemAllLine;
    private javax.swing.JMenuItem itemClear;
    private javax.swing.JMenuItem itemData;
    private javax.swing.JMenuItem itemExit;
    private javax.swing.JRadioButtonMenuItem itemKruskal;
    private javax.swing.JRadioButtonMenuItem itemNone;
    private javax.swing.JRadioButtonMenuItem itemPrim;
    private javax.swing.JMenuItem itemRedo;
    private javax.swing.JMenuItem itemTentangkami;
    private javax.swing.JMenuItem itemUndo;
    private javax.swing.JLabel labelJudul;
    private javax.swing.JMenu menuFile;
    private javax.swing.JMenu menuPertolongan;
    private javax.swing.JMenu menuPohon;
    private javax.swing.JMenu menuTampilkan;
    private javax.swing.JMenu menuUbah;
    private javax.swing.JMenuBar menubar;
    private minimum.spanning.tree.PanelTree panelTree;
    // End of variables declaration//GEN-END:variables

}
